<template>
	<view class="content">
		<f-navbar title="个人信息" fontColor="#333" fontSize="34" bgColor="#fff">
			<view class="u-flex" slot="left">
				<van-icon name="arrow-left" color="#333" size="20"/>
			</view>
		</f-navbar>
		<view class="content_box">
			<view class="content_topimg" @click="onUploadavatar">
				<view class="content_toptitle">头像</view>
				<view class="content_topR">
					<view class="content_img">
						<image class="avatarimg" :src="avatarimg" mode=""></image>
					</view>
					<van-icon name="arrow" size="16" color="#999" />
				</view>
			</view>
			<view class="content_namebox" @click="onnickname">
				<view class="content_name">昵称</view>
				<view class="content_nameR">{{nickname}}
					<van-icon class="right_icon" name="arrow" size="16" color="#999" />
				</view>
			</view>
			<view class="content_namebox" @click="onphonenum">
				<view class="content_name">手机号</view>
				<view class="content_nameR">{{phonenum}}
					<van-icon class="right_icon" name="arrow" size="16" color="#999" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import fNavbar from '@/uni_modules/f-navbar/components/f-navbar/f-navbar.vue';
	import {getBaseUserInfo,setUserInfo} from '../../../utils/app.js'
	export default {
		components: {
			fNavbar
		},
		data() {
			return {
				avatarimg:'https://yuanchubao.oss-cn-beijing.aliyuncs.com/no-cart.png',
				nickname:'微信用户',
				apiUrl: getApp().globalData.apiUrl,
				phonenum:'请输入手机号码'
			}
		},
		onLoad() {
			this.getList()
		},
		methods: {
			// 获取数据
			getList(){
				getBaseUserInfo({
				}).then(res => {
					console.log(res);		
						this.avatarimg = res.data.avatar
						this.nickname = res.data.nickname
						this.phonenum = res.data.phone==''?'请输入手机号码':res.data.phone
				})
			},
			// 跳转到修改手机号页面
			onphonenum(){
				uni.navigateTo({
					url:'/pages/subPack/editphone/editphone'
				})
			},
			// 跳转到修改昵称页面
			onnickname(){
				uni.navigateTo({
					url:'/pages/subPack/nickname/nickname?name='+this.nickname
				})
			},
			
			
			// 上传头像
			onUploadavatar() {

				uni.chooseImage({
					sourceType: ['camera','album'], //从相册选择
					success: chooseImageRes => {
						console.log('成功', chooseImageRes);
						const tempFilePaths = chooseImageRes.tempFilePaths[0];
						console.log(tempFilePaths);
						// this.avatarimg = tempFilePaths
						uni.uploadFile({
							url: this.apiUrl+'api/uploadFile', //仅为示例，非真实的接口地址
							filePath: tempFilePaths,
							name: 'file',
							// header: {
							// 	accessToken: uni.getStorageSync('accessToken'),
							// 	platform: 2,
							// 	type: 1
							// },
							formData: {
								file:tempFilePaths,
								type:'1'
							},
							success: res => {
								console.log(res);
								let data1 = JSON.parse(res.data)
								console.log(data1);
								if(data1.code==1){
									setUserInfo({
										fields:data1.data.url,
										type:2
									}).then(res1 => {
										if(res1.code==1){
											this.getList()
										}
									})
								}
								// console.log('上传成功', JSON.parse(res.data));
								// uploadFile上传成功后，根据和后台的约定msgCode判断接口调用状态
				
							}
						});
					},
					// fail: err => {
					// 	this.myToast('图片上传失败', 'none');
					// }
				});
			},
		},
		onShow:function() {
			var that = this;
			var pages = getCurrentPages();
			var currPage = pages[pages.length - 1]; //当前页面
			let res = currPage.data.userdata;
			if(res!==undefined){
			console.log(res)//为传过来的值
			this.nickname = res.name	
			this.phonenum = res.phonenum
			}
	
		}
	}
</script>

<style>
	page {
		background: #eee;
	}

	.content_topimg {
		width: 100%;
		height: 109rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 4rpx;
		background: #fff;
	}

	.content_toptitle {
		font-size: 28rpx;
		font-family: Hiragino Sans GB;
		font-weight: 600;
		color: #333333;
		margin-left: 30rpx;
	}

	.content_topR {
		display: flex;
		align-items: center;
		margin-right: 30rpx;
	}

	.content_img {
		width: 68rpx;
		height: 68rpx;
		/* background: #88ABDA; */
		border-radius: 50%;
		margin-right: 16rpx;
		z-index: 99;
	}

	.content_namebox {
		width: 100%;
		height: 109rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #fff;
		border-top: 1rpx solid #eee;
	}

	.content_name {
		font-size: 28rpx;
		font-family: Hiragino Sans GB;
		font-weight: 600;
		color: #333333;
		margin-left: 30rpx;
	}

	.content_nameR {
		/* display: flex;
		align-items: center; */
		font-size: 28rpx;
		font-family: Hiragino Sans GB;
		font-weight: normal;
		color: #BBBBBB;
		margin-right: 30rpx;
	}

	.right_icon {
		margin-left: 8rpx;
	}
	.avatarimg{
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}
</style>